/*
 * @Description:
 * @Author: qingyang
 * @Date: 2023-02-27 09:50:22
 * @LastEditors: qingyang
 * @LastEditTime: 2023-03-07 16:21:53
 */
import { reactive, markRaw, ref } from 'vue'
import { removeUndefinedParams } from '@/utils/index'
import type {
  CPageTableProps,
  ColumnSettingTypeEnum,
  SearchOptionsType
} from '@/components/CPageTable'
import type { BasicPagingListParams } from '@/apis/basicModel'
import { getInventoryList } from '@/apis/eci/inventory/inventory'
import { getCurrentOrganizationList } from '@/apis/common/common'
import { InventoryStatusEnum, InventoryStatusOptions } from '@/constants/enum'
import { useRoute } from 'vue-router'

export const useInventory = () => {
  const route = useRoute()
  // 搜索字段及其他
  const searchForm = reactive({
    organizationCode: '',
    applicableYear: route.query?.applicableYear as string | undefined,
    createTime: [],
    status: '' as unknown as keyof typeof InventoryStatusEnum
  })

  // 搜索配置
  const searchOptions: SearchOptionsType[] = markRaw([
    {
      name: '组织名称',
      placeholder: '组织名称',
      field: 'organizationCode',
      type: 'select',
      remoteMethod: (query: string): Promise<any> => {
        console.log('remoteMethod', query)
        const params = {
          organizationName: query
        }
        return getCurrentOrganizationList(params).then((res) => {
          const ans = res.map((item) => {
            return {
              label: item.organizationName,
              value: item.organizationCode
            }
          })
          return ans
        })
      },
      style: 'width: 192px'
    },
    {
      name: '适用年份',
      field: 'applicableYear',
      type: 'datePicker',
      dateType: 'year',
      format: 'YYYY'
    },
    {
      name: '发布状态',
      field: 'status',
      type: 'select',
      options: InventoryStatusOptions
    },
    {
      name: '创建时间',
      field: 'createTime',
      type: 'datePicker',
      dateType: 'datetimerange',
      startPlaceholder: '创建起始时间',
      endPlaceholder: '创建截止时间',
      format: 'YYYY-MM-DD HH:mm:ss'
    }
  ])

  // 表格配置
  const tableOptions: CPageTableProps['tableOptions'] = {
    loadData
  }

  function loadData(parameter: BasicPagingListParams) {
    console.log('loadData', searchForm)
    const params = Object.assign(handleParams(), parameter)
    return getInventoryList(params)
  }

  // 处理参数
  const handleParams = () => {
    const [startTime, endTime] = searchForm.createTime || []
    // 参数
    let params = {
      organizationCode: searchForm?.organizationCode,
      applicableYear: searchForm?.applicableYear,
      status: searchForm?.status,
      startTime,
      endTime
    }
    params = removeUndefinedParams(params)
    return params
  }

  const tableFieldsSetting: CPageTableProps['tableFieldsSetting'] = [
    {
      title: '组织名称/监测计划名称',
      field: 'inventoryName',
      slot: 'inventoryNameSlot',
      minWidth: '160',
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      title: '适用年份',
      field: 'applicableYear',
      width: '85',
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      title: '创建人',
      field: 'creator',
      minWidth: '120',
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      title: '创建时间',
      field: 'createTime',
      minWidth: '180',
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      title: '填报任务',
      field: 'sendLog',
      align: 'center',
      slot: 'sendLogSlot',
      width: '100',
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      title: '发布时间',
      field: 'releaseTime',
      minWidth: '180',
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      key: 'status',
      title: '发布状态',
      field: 'status',
      slot: 'statusSlot',
      fixed: 'right',
      width: '85',
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      key: 'op',
      title: '操作',
      field: 'op',
      slot: 'opSlot',
      width: '230',
      fixed: 'right',
      columnSettingConfig: {
        enabled: true,
        type: ColumnSettingTypeEnum.FIXED
      }
    }
  ]

  const options = reactive<CPageTableProps>({
    title: '监测计划管理',
    searchOptions,
    tableOptions,
    tableFieldsSetting,
    enableFieldsSetting: true
  })

  const activeTab = ref('TAB1')

  return {
    // 头部右侧筛选项配置
    searchOptions,
    // 筛选项字段
    searchForm,
    // 表格配置
    tableOptions,
    // 配置
    options,
    activeTab
  }
}
